package com.css.fxfzypg.modules.resultsManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.preassesstaskmanage.vo.PreVo;
import com.css.fxfzypg.modules.resultsManagement.entity.MapManagementEntity;
import com.css.fxfzypg.modules.resultsManagement.queryParams.MapManagementQueryParams;
import com.css.fxfzypg.modules.resultsManagement.service.MapManagementService;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/map")
@Slf4j
public class MapManagementController {

    @Resource
    MapManagementService mapManagementService;


    /**
     * 新增
     *
     * @param mapManagementEntity
     * @return
     */
    @PostMapping("/saveMap")
    public RestResponse saveMap(@RequestBody MapManagementEntity mapManagementEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String mapId = mapManagementService.saveMap(mapManagementEntity);
            jsonObject.put("message", "保存成功");
            jsonObject.put("mapId", mapId);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            log.error("新增图件数据失败", e);
            restResponse = RestResponse.fail("新增图件数据失败!");
        }
        return restResponse;
    }

    /**
     * id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public RestResponse getById(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(mapManagementService.getById(id));
        } catch (Exception e) {
            log.error("查询图件数据失败", e);
            restResponse = RestResponse.fail("查询图件数据失败");
        }
        return restResponse;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @PostMapping("/deleteByIds/{ids}")
    public RestResponse deleteByIds(@PathVariable List<String> ids) {
        RestResponse restResponse = null;
        try {
            mapManagementService.deleteByIds(ids);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("删除图件数据失败");
            restResponse = RestResponse.fail("删除图件数据失败");
        }
        return restResponse;
    }

    /**
     * 修改
     *
     * @param mapManagementEntity
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody MapManagementEntity mapManagementEntity) {
        RestResponse restResponse = null;
        try {
            MapManagementEntity mapManagementEntity1 = mapManagementService.update(mapManagementEntity);
            restResponse = RestResponse.succeed(mapManagementEntity1);
        } catch (Exception e) {
            log.error("修改图件数据失败");
            restResponse = RestResponse.fail("修改图件数据失败");
        }
        return restResponse;
    }

    /**
     * 分页多条件查询
     *
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/queryMaps")
    public RestResponse queryMaps(HttpServletRequest request, MapManagementQueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = mapManagementService.queryMaps(queryParams, curPage, pageSize);
            response = RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("查询图件数据失败!", e);
            response = RestResponse.fail("查询图件数据失败!");
        }
        return response;
    }

    /**
     * 根据任务id与图件标签查询
     *
     * @param mapManagementEntity
     * @return
     */
    @GetMapping("/getByLabel")
    public RestResponse getByLabel(MapManagementEntity mapManagementEntity) {
        RestResponse restResponse = null;
        try {
            MapManagementEntity mapManagementEntity1 = mapManagementService.getByLabel(mapManagementEntity.getTaskId(), mapManagementEntity.getMapLabel());
            restResponse = RestResponse.succeed(mapManagementEntity1);
        } catch (Exception e) {
            log.error("查询图件数据失败", e);
            restResponse = RestResponse.fail("查询图件数据失败");
        }
        return restResponse;
    }

    /**
     * 获取未结束的所有父任务
     *
     * @return
     */
    @GetMapping("/getFuTask")
    public RestResponse getFuTask() {
        RestResponse response = null;
        try {
            List<PreVo> list = mapManagementService.getFuTask();
            response = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 用任务id获得分布图
     * @param taskId
     * @return
     */
    @GetMapping("/getTu/{id}")
    public RestResponse getTu(@PathVariable("id") String taskId) {
        RestResponse restResponse = null;
        try {

            String populationPath = mapManagementService.getPopulationPath(taskId);
            String economicsPath = mapManagementService.getEconomicsPath(taskId);

            restResponse = RestResponse.succeed(economicsPath);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }
}
